![Introducing Dashboard Analytics](https://cdn.sanity.io/images/cgdhsj6q/production/af327c5bd94708db501516a75da6870aef894c64-2286x1522.png?w=400&fit=max&auto=format)
Product
Introducing Dashboard Analytics
We're introducing a new Analytics feature in the Socket dashboard so you can view changes in your organization's and repositories' alerts over time.
@reach/portal
Advanced tools
@reach/portal is a React component that allows you to render children into a DOM node that exists outside the DOM hierarchy of the parent component. This is useful for creating modals, tooltips, and other UI elements that need to visually break out of their container.
Basic Portal Usage
This example demonstrates the basic usage of the @reach/portal package. The `Portal` component is used to render a div outside of the parent DOM hierarchy, which can be useful for creating modals or tooltips.
import React from 'react';
import { Portal } from '@reach/portal';
function App() {
return (
<div>
<h1>My App</h1>
<Portal>
<div style={{ position: 'absolute', top: 0, left: 0, background: 'white', border: '1px solid black' }}>
This is rendered in a portal!
</div>
</Portal>
</div>
);
}
export default App;
Nested Portals
This example shows how you can nest portals within each other. The nested `Portal` component renders its children outside of the parent DOM hierarchy, allowing for complex UI structures.
import React from 'react';
import { Portal } from '@reach/portal';
function App() {
return (
<div>
<h1>My App</h1>
<Portal>
<div style={{ position: 'absolute', top: 0, left: 0, background: 'white', border: '1px solid black' }}>
This is rendered in a portal!
<Portal>
<div style={{ position: 'absolute', top: 20, left: 20, background: 'lightgray', border: '1px solid black' }}>
This is a nested portal!
</div>
</Portal>
</div>
</Portal>
</div>
);
}
export default App;
The `react-dom` package provides the `createPortal` method, which allows you to render children into a DOM node that exists outside the DOM hierarchy of the parent component. It is a core part of React and offers similar functionality to @reach/portal but is more integrated into the React ecosystem.
The `react-portal` package is another popular library for creating portals in React. It offers a simple API and additional features like event handling and portal management. It is more feature-rich compared to @reach/portal but may be overkill for simple use cases.
The `react-reverse-portal` package allows you to create portals that can be moved around the DOM dynamically. It provides more advanced features for managing portal content and is useful for complex applications that require dynamic portal management. It offers more flexibility compared to @reach/portal.
Creates and appends a DOM node to the end of document.body
and renders a React tree into it. Useful for rendering a natural React element hierarchy with a different DOM hierarchy to prevent parent styles from clipping or hiding content (for popovers, dropdowns, and modals).
import Portal from "@reach/portal";
function Example() {
return (
<Portal>
<div>Stuff goes here</div>
</Portal>
);
}
FAQs
Declarative portals for React
The npm package @reach/portal receives a total of 246,392 weekly downloads. As such, @reach/portal popularity was classified as popular.
We found that @reach/portal demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
We're introducing a new Analytics feature in the Socket dashboard so you can view changes in your organization's and repositories' alerts over time.
Security News
A new OpenSSF report uncovers critical gaps in secure software training, with 75% of new developers unfamiliar with secure practices, highlighting urgent educational needs.
Security News
The 2023 Python Developers Survey reveals key trends in packaging, web frameworks, and developer demographics, highlighting a shift toward innovative tools as the Python community diversifies and grows among less experienced developers.